package com.example.shw.wedgit

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.view.View
import com.example.shw.R
import java.util.*

class PlaneView(context: Context) : View(context){
    var currentX : Float = 0.toFloat()
    var currentY : Float = 0.toFloat()
    val p = Paint()
    internal var plane0 : Bitmap
    internal var plane1 : Bitmap
    private var index = 0
    init {
        plane0 = BitmapFactory.decodeResource(context.getResources(), R.drawable.hero1)
        plane1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.hero2)
        Timer().schedule(object : TimerTask(){
            override fun run() {
                index++
                this@PlaneView.invalidate()
            }
        }, 0L, 100L)
        isFocusable = true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawBitmap(if (index % 2 == 0) plane0 else plane1, currentX, currentY, p)
    }
}